﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bigger.Framework;
using Bigger.Base.Cache;
using System.Web.Mvc;
using Bigger.Base;
using CJAtech.BBS.Web.Models;
using CJAtech.BBS.Process;

namespace CJAtech.BBS.Web.Controllers
{
    public partial class GuangQiController
    {
        /// <summary>
        /// 轮询是否有最新评论
        /// </summary>
        /// <param name="id"></param>
        /// <param name="topId"></param>
        /// <returns></returns>
        public ActionResult CommentLast(int? id, int? commentID)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (!id.HasValue)
            {
                objInvokeResult.ResultCode = "IDEmpty";
                objInvokeResult.ResultMessage = "对不起，文章不存在";
                return Json(objInvokeResult);
            }

            var CurrentCache = Bigger.Core.Cache.CacheHelper.CacheInstance;
            string cacheKey = string.Format("Article_{0}_LatestComment>{1}", id, commentID);
            object obj = CurrentCache.GetCache(cacheKey);
            if (obj == null)
            {
                BizCommentInfo objBizCommentInfo = new BizCommentInfo();
                obj = HasLastedComment(objBizCommentInfo.DbContext, id.Value, commentID);
            }
            if (obj == null)
            {
                objInvokeResult.ResultCode = "HasNoLast";
                objInvokeResult.ResultMessage = "对不起，没有最新评论";
                return Json(objInvokeResult);
            }
            CurrentCache.SetCache(cacheKey, obj, TimeSpan.FromSeconds(30));

            objInvokeResult.ResultMessage = "有最新的评论";
            return Json(objInvokeResult);
        }
        /// <summary>
        /// 加载评论列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CommentList(int? id)
        {
            if (!id.HasValue)
            {
                return Content("");
            }
            InvokeResult objInvokeResult = CheckUserTicket("tjzheng");
            if (objInvokeResult.ResultCode != "Success")
            {
                return Content("");
            }
            ViewBag.id = id.Value;
            BizCommentInfo objBizCommentInfo = new BizCommentInfo();
            List<CommentInfo> objCommentInfoList = null;
            objCommentInfoList = objBizCommentInfo.GetList("ObjectId='" + id + "' AND IsAudit=1", "CommentId desc", "top 10 *");

            UserInfo objUserInfo = objInvokeResult.Data as UserInfo;
            LoadDiggInfoList(objCommentInfoList, objUserInfo);

            return View("CommentList", objCommentInfoList);
        }

        /// <summary>
        /// 获取热门评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CommentHotList(int? id, int? page)
        {
            if (!id.HasValue)
            {
                return Content("");
            }
            InvokeResult objInvokeResult = CheckUserTicket("tjzheng");
            if (objInvokeResult.ResultCode != "Success")
            {
                return Content("");
            }
            ViewBag.id = id.Value;
            BizCommentInfo objBizCommentInfo = new BizCommentInfo();
            if (!page.HasValue || page < 1)
            {
                page = 1;
            }
            List<CommentInfo> objCommentInfoList = objBizCommentInfo.GetPageListCache("", TimeSpan.FromSeconds(30), page.Value - 1, 10, "ObjectId='" + id + "' AND IsAudit=1", "DiggCount desc");
            if (objCommentInfoList.IsNull())
            {
                return Content("");
            }

            UserInfo objUserInfo = objInvokeResult.Data as UserInfo;
            LoadDiggInfoList(objCommentInfoList, objUserInfo);

            return View("CommentList", objCommentInfoList);
        }

        [NonAction]
        private void LoadDiggInfoList(List<CommentInfo> objCommentInfoList, UserInfo objUserInfo)
        {
            BizDiggInfo objBizDiggInfo = new BizDiggInfo();
            string objectIDs = objCommentInfoList.Select(c => c.CommentId).JoinComma().ConvertVarcharString();
            ViewBag.DiggInfoList = objBizDiggInfo.GetListCache("", TimeSpan.FromSeconds(5), string.Format("ObjectId in({0}) AND UserId='{1}'", objectIDs, objUserInfo.UserId));
        }
        /// <summary>
        /// 加载页面上最新一条评论之后的评论列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="commentID"></param>
        /// <returns></returns>
        public ActionResult CommentTopList(int? id, int? commentID)
        {
            if (!id.HasValue)
            {
                return Content("");
            }
            InvokeResult objInvokeResult = CheckUserTicket("tjzheng");
            if (objInvokeResult.ResultCode != "Success")
            {
                return Content("");
            }
            BizCommentInfo objBizCommentInfo = new BizCommentInfo();
            List<CommentInfo> objCommentInfoList = null;
            if (!commentID.HasValue)
            {
                objCommentInfoList = objBizCommentInfo.GetListCache("", TimeSpan.FromSeconds(5), "ObjectId='" + id + "' AND IsAudit=1", "CommentId desc", "top 10 *");
                //objCommentInfoList = objBizCommentInfo.GetList("ObjectId='" + id + "' AND IsAudit=1", "CommentId desc", "top 10 *");
            }
            else
            {
                objCommentInfoList = objBizCommentInfo.GetListCache("", TimeSpan.FromSeconds(5), "ObjectId='" + id + "' AND IsAudit=1 AND CommentId>" + commentID, "CommentId asc", "top 10 *");
                //objCommentInfoList = objBizCommentInfo.GetList("ObjectId='" + id + "' AND IsAudit=1 AND CommentId>" + commentID, "CommentId asc", "top 10 *");
            }
            ViewBag.id = id.Value;
            if (objCommentInfoList.IsNull())
            {
                return Content("");
            }

            UserInfo objUserInfo = objInvokeResult.Data as UserInfo;
            LoadDiggInfoList(objCommentInfoList, objUserInfo);

            return View("CommentList", objCommentInfoList);
        }
        /// <summary>
        /// 加载上一条评论的评论列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="commentID"></param>
        /// <returns></returns>
        public ActionResult CommentBottomList(int? id, int? commentID)
        {
            if (!id.HasValue)
            {
                return Content("");
            }
            if (!commentID.HasValue)
            {
                return Content("");
            }
            InvokeResult objInvokeResult = CheckUserTicket("tjzheng");
            if (objInvokeResult.ResultCode != "Success")
            {
                return Content("");
            }
            ViewBag.id = id.Value;

            BizCommentInfo objBizCommentInfo = new BizCommentInfo();
            List<CommentInfo> objCommentInfoList = null;
            objCommentInfoList = objBizCommentInfo.GetListCache("", TimeSpan.FromSeconds(30), "ObjectId='" + id + "' AND IsAudit=1 AND CommentId<" + commentID, "CommentId desc", "top 10 *");
            //var objCommentInfoList = objBizCommentInfo.GetList("ObjectId=" + id + " AND IsAudit=1 AND CommentId<" + commentID, "CommentId desc", "top 10 *");
            //var objCommentInfoList = objBizCommentInfo.GetLimitList(0, 10, "ObjectId=" + id + " AND IsAudit=1 AND CommentId<" + commentID, "CreateDate desc");
            if (objCommentInfoList.IsNull())
            {
                return Content("");
            }

            UserInfo objUserInfo = objInvokeResult.Data as UserInfo;
            LoadDiggInfoList(objCommentInfoList, objUserInfo);

            return View("CommentList", objCommentInfoList);
        }

        private object HasLastedComment(DbContext objDbContext, int id, int? CommentId)
        {
            if (CommentId.HasValue)
            {
                return objDbContext.ExecuteScalar("select top 1 CommentId from BBS_CommentInfo_TB with(nolock) where commentid>@CommentId AND ObjectId=@ObjectId", new { CommentId = CommentId, ObjectId = id });
            }
            return objDbContext.ExecuteScalar("select top 1 CommentId from BBS_CommentInfo_TB with(nolock) Where ObjectId=@ObjectId", new { ObjectId = id });
        }
    }
}